package com.lbssoft.gpsbd.controller;

import com.lbssoft.gpsbd.annotation.LogAnnotation;
import com.lbssoft.gpsbd.model.Permission;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.PermissionSort;
import com.lbssoft.gpsbd.service.PermissionService;
import com.lbssoft.gpsbd.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;


@Api("资源管理")
@RestController
@RequestMapping("/permissions")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;

	@Autowired
	private RoleService roleService;

	@ApiOperation(value="获取资源列表", notes="获取资源列表")
	@RequiresPermissions("system:permis:view")
	@RequestMapping(value = "/{groupId}",method = {RequestMethod.GET})
	@ResponseBody
	public Result list(@NotNull @PathVariable int groupId) {
		Result result = new Result();
		result.setData(permissionService.list(groupId));
		return result;
	}

	@ApiOperation(value="添加权限", notes="添加权限")
	@RequiresPermissions("system:permis:add")
	@LogAnnotation(targetType = "permis",action = "add",remark = "添加权限")
	@RequestMapping(value = "",method = {RequestMethod.POST})
	@ResponseBody
	public Result add(@Validated @RequestBody Permission permission) {
		Result result = new Result();
		if(permission.getParentId()==0&&permission.getType()==1){
			result.setCode(1);
			result.setMsg("按钮不能所属顶级菜单");
			return result;
		}
		permissionService.add(permission);
		return result;
	}

	@ApiOperation(value="删除权限", notes="删除权限")
	@RequiresPermissions("system:permis:delete")
	@LogAnnotation(targetType = "permis",action = "delete",remark = "删除权限")
	@RequestMapping(value = "/{id}",method = {RequestMethod.DELETE})
	@ResponseBody
	public Result delete(@NotNull @PathVariable int id) {
		Result result = new Result();
		List<Permission> list = permissionService.getPermissionsByPid(id);
		if(list.size()>0){
			result.setCode(1);
			result.setMsg("此权限包含子权限不可删除");
			return result;
		}
		permissionService.delete(id);
		return result;
	}

	@ApiOperation(value="修改权限信息", notes="修改权限信息")
	@ApiImplicitParam(paramType="body",name = "permission", value = "角色对应的权限", dataType = "Permission")
	@RequiresPermissions("system:permis:update")
	@LogAnnotation(targetType = "permis",action = "update",remark = "修改权限信息")
	@RequestMapping(value = "",method = {RequestMethod.PUT})
	@ResponseBody
	public Result update(@Validated @RequestBody Permission permission) {
		Result result = new Result();
		if(permission.getId()==permission.getParentId()){
			result.setCode(1);
			result.setMsg("所属节点不能是自己");
			return result;
		}
		if(permission.getParentId()==0&&permission.getType()==1){
			result.setCode(1);
			result.setMsg("按钮不能所属顶级菜单");
			return result;
		}
			permissionService.update(permission);
		return result;
	}

	@ApiOperation(value="菜单权限排序", notes="菜单权限排序")
	@ApiImplicitParam(paramType="body",name = "sort", value = "权限", dataType = "PermissionSort")
	@RequiresPermissions("system:permis:update")
	@LogAnnotation(targetType = "permis",action = "update",remark = "菜单权限排序")
	@RequestMapping(value = "sort",method = {RequestMethod.PUT})
	@ResponseBody
	public Result sort(@Validated @RequestBody PermissionSort sort) {
		return permissionService.sort(sort.getId(),sort.getDirection());
	}

}